package com.uitls.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ProgressBar;

public class AnimationUtil {
    //缩放
    public static void scaleInAnimation(View view, float fromScale, float toScale) {
        if (view == null) {
            return;
        }
        Animation scaleAnimation = new ScaleAnimation(fromScale, toScale, fromScale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);
        scaleAnimation.setInterpolator(new AccelerateInterpolator());
        scaleAnimation.setFillEnabled(true);
        scaleAnimation.setFillAfter(true);
        view.startAnimation(scaleAnimation);
    }

    public static void scaleInAnimation(View view, float fromX, float toX, float fromY, float toY) {
        if (view == null) {
            return;
        }
        Animation scaleAnimation = new ScaleAnimation(fromX, toX, fromY, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(100);
        scaleAnimation.setInterpolator(new AccelerateInterpolator());
        scaleAnimation.setFillEnabled(true);
        scaleAnimation.setFillAfter(true);
        view.startAnimation(scaleAnimation);
    }

    //移动
    public static void translateInAnimation(View view, float fromYDelta, float toYDelta) {
        if (view == null) {
            return;
        }
        Animation translateAnimation = new TranslateAnimation(0, 0, fromYDelta, toYDelta);
        translateAnimation.setDuration(300);
        translateAnimation.setInterpolator(new AccelerateInterpolator());
        view.startAnimation(translateAnimation);
    }

    //渐变
    public static void alphaInAnimation(View view, float fromAlpha, float toAlpha, long startOffset) {
        AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
        animation.setDuration(100);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setStartOffset(startOffset);
        animation.setFillEnabled(true);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }

    /**
     * 从左到右出现
     *
     * @param view
     */
    public static void startSlideFromLeftAnimation(View view, long durationMillis, AnimationListener listener) {
        if (view == null) {
            return;
        }
        Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setAnimationListener(listener);
        view.startAnimation(animation);
    }

    /**
     * 从下到上出现
     *
     * @param view
     */
    public static void startFromBottomAnimation(View view, long durationMillis, AnimationListener listener) {
        if (view == null) {
            return;
        }
        Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setAnimationListener(listener);
        animation.setDuration(durationMillis);
        view.startAnimation(animation);
        return;
    }

    /**
     * 从上到下消失
     *
     * @param view
     */
    public static void startToBottomAnimation(View view, long durationMillis, AnimationListener listener) {
        if (view == null) {
            return;
        }
        Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        animation.setAnimationListener(listener);
        animation.setDuration(durationMillis);
        view.startAnimation(animation);
        return;
    }


    /**
     * 多个动画
     *
     * @param view
     * @param animationArgs
     */
    public static void startAnimation(View view, Animation... animationArgs) {
        startAnimation(view, null, animationArgs);
    }

    /**
     * 多个动画
     *
     * @param view
     * @param animationArgs
     */
    public static void startAnimation(View view, AnimationListener listener, Animation... animationArgs) {
        AnimationSet animationSet = new AnimationSet(true);
        for (Animation animation : animationArgs) {
            if (animation != null) {
                animationSet.addAnimation(animation);
            }
        }
        animationSet.setDuration(100);
        animationSet.setInterpolator(new AccelerateInterpolator());
        animationSet.setFillEnabled(true);
        animationSet.setFillAfter(true);
        animationSet.setAnimationListener(listener);
        view.startAnimation(animationSet);
    }

    public static Animation getTranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta, long durationMillis) {
        Animation translateAnimation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
        translateAnimation.setDuration(durationMillis);
        translateAnimation.setInterpolator(new AccelerateInterpolator());
        return translateAnimation;
    }

    public static Animation getAlphaAnimation(float fromAlpha, float toAlpha, long durationMillis, long startOffset) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(fromAlpha, toAlpha);
        alphaAnimation.setDuration(durationMillis);
        alphaAnimation.setStartOffset(startOffset);
        alphaAnimation.setInterpolator(new AccelerateInterpolator());
        return alphaAnimation;
    }

    public static Animation getScaleAnimation(float fromScale, float toScale, long durationMillis) {
        Animation scaleAnimation = new ScaleAnimation(fromScale, toScale, fromScale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(durationMillis);
        scaleAnimation.setInterpolator(new AccelerateInterpolator());
        return scaleAnimation;
    }

    public static void setProgressChangeAnimation(final ProgressBar progressBar, int toProgress) {
//		LogUtil.w("ANIMATE ", "Old progress  = " + progressBar.getProgress() + " | toProgress = " + toProgress);
        if (progressBar.getProgress() > toProgress) {
            return;
        }

        ObjectAnimator progressAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), toProgress);
        progressAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        progressAnimator.setDuration(500);
        if (toProgress == 100) {
            progressAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    progressBar.animate().alpha(0).withEndAction(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                        }
                    });
                }
            });
        }
        progressAnimator.start();
    }

    //旋转
    public static void rotationInAnimation(View view, float fromScale, float toScale) {
        if (view == null) {
            return;
        }
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "rotation", fromScale, toScale);
        anim.setDuration(3000);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.setRepeatMode(ValueAnimator.RESTART);
        anim.setInterpolator(new LinearInterpolator());
        anim.start();
    }

    //旋转
    public static ObjectAnimator getRotationInAnimation(View view, float fromScale, float toScale, int duration) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "rotation", fromScale, toScale);
        anim.setDuration(duration);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.setRepeatMode(ValueAnimator.RESTART);
        anim.setInterpolator(new LinearInterpolator());
        return anim;
    }

    public static ObjectAnimator getTranslation(int from, int to){
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                from,"translationY",to);
        objectAnimator.setDuration(1000);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        return objectAnimator;
    }


}
